package com.mh.shiro.business.service.impl;

import com.mh.shiro.business.entity.RoleResources;
import com.mh.shiro.business.entity.UserRole;
import com.mh.shiro.business.service.SysRoleResourcesService;
import com.mh.shiro.business.service.SysRoleService;
import com.mh.shiro.business.service.SysUserRoleService;
import com.mh.shiro.persistence.beans.SysRoleResources;
import com.mh.shiro.persistence.mapper.SysRoleResourcesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年06月24日21:48
 * @Description: <描述>
 */
@Service
public class SysRoleResourcesServiceImpl implements SysRoleResourcesService {

    @Autowired
    private SysRoleResourcesMapper resourcesMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
    public void removeByRoleId(Long roleId) {
        //删除
        Example example = new Example(SysRoleResources.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("roleId", roleId);

        resourcesMapper.deleteByExample(example);
    }

    /**
     * 添加角色资源
     *
     * @param roleId
     * @param resourcesId
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
    public void addRoleResources(Long roleId, String resourcesId) {
        //删除
        removeByRoleId(roleId);
        //添加
        if(!StringUtils.isEmpty(resourcesId)) {
            String[] resourcesArr = resourcesId.split(",");
            if (resourcesArr.length == 0) {
                return;
            }
            SysRoleResources r = null;
            List<SysRoleResources> roleResources = new ArrayList<>();
            for(String ri : resourcesArr) {
                if(StringUtils.isEmpty(ri)) {
                    continue;
                }
                r = new SysRoleResources();
                r.setRoleId(roleId);
                r.setResourcesId(Long.parseLong(ri));
                r.setCreateTime(new Date());
                r.setUpdateTime(new Date());

                roleResources.add(r);
            }
            if(roleResources.size() == 0) {
                return;
            }
            resourcesMapper.insertList(roleResources);
        }


    }

    @Override
    public RoleResources insert(RoleResources entity) {
        return null;
    }

    @Override
    public void insertList(List<RoleResources> entities) {

    }

    @Override
    public boolean removeByPrimaryKey(Long primaryKey) {
        return false;
    }

    @Override
    public boolean update(RoleResources entity) {
        return false;
    }

    @Override
    public boolean updateSelective(RoleResources entity) {
        return false;
    }

    @Override
    public RoleResources getByPrimaryKey(Long primaryKey) {
        return null;
    }

    @Override
    public RoleResources getOneByEntity(RoleResources entity) {
        return null;
    }

    @Override
    public List<RoleResources> listAll() {
        return null;
    }

    @Override
    public List<RoleResources> listByEntity(RoleResources entity) {
        return null;
    }
}
